<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>js速度动画</title>
    <style>
        body,div,span{
            margin: 0;
            padding: 0;
        }
        #div1{
            width: 200px;
            height: 200px;
            background: red;
            position: relative;
            left: -200px;
            top: 0;
        }
        #div1 span{
            width: 20px;
            height: 50px;
            background: #73c4f3;
            position: absolute;
            left: 200px;
            top: 75px;
            color: seashell;
        }
    </style>
    <script>
        window.onload = function(){
            console.log('window move');
            var oDiv = document.getElementById('div1');
            // 鼠标放置，开始外拉动画
            oDiv.onmouseover = function() {
                startMove(0);
            }
            // 鼠标离开，开始收回动画
            oDiv.onmouseleave = function() {
                startMove(-200);
            }
        }
        var timer = null;
        /* js速度动画*/
        function startMove(iTarget) {
            console.log('start move');
            clearInterval(timer);
            var oDiv = document.getElementById('div1');
            timer = setInterval(function() {
                let speed = oDiv.offsetLeft > iTarget? -10: 10; 
                if(oDiv.offsetLeft === iTarget){
                    clearInterval(timer);
                } else{
                    oDiv.style.left = oDiv.offsetLeft + speed + 'px';
                }
            }, 30)
        }
    </script>
</head>
<body>
    <div id="div1">
        <span id="share">分享</span>
    </div>
</body>
</html>